package com.lam.common.components.easyexcel.converter;

import java.math.BigDecimal;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.lam.common.utils.DateUtil;

public class TimeLongConverter implements Converter<Long> {

	@Override
	public Class<?> supportJavaTypeKey() {
		return Long.class;
	}

	@Override
	public CellDataTypeEnum supportExcelTypeKey() {
		return CellDataTypeEnum.NUMBER;
	}

	/**
	 * 这里读的时候会调用
	 *
	 * @param cellData            NotNull
	 * @param contentProperty     Nullable
	 * @param globalConfiguration NotNull
	 * @return
	 */
	@Override
	public Long convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
			GlobalConfiguration globalConfiguration) {
		
		BigDecimal number = cellData.getNumberValue();
		return number == null ? null : number.longValue();
	}

	/**
	 * 这里是写的时候会调用
	 *
	 * @param value               NotNull
	 * @param contentProperty     Nullable
	 * @param globalConfiguration NotNull
	 * @return
	 */
	@Override
	public WriteCellData<?> convertToExcelData(Long value, ExcelContentProperty contentProperty,
			GlobalConfiguration globalConfiguration) {
		
		return new WriteCellData<>(DateUtil.convertTimeLong(value));
	}
}
